package com.changgou.controller;

import com.changgou.entity.Result;
import com.changgou.pojo.FastDFSFile;
import com.changgou.util.FastDFSClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <dl>
 * <dd>描述: ~节点业务逻辑实现</dd>
 * <dd>创建时间：  10:13 2020/5/12</dd>
 * <dd>创建人： zz</dd>
 * <dt>版本历史: </dt>
 * <pre>
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2020/5/12      guodong       1.0        1.0 Version
 * </pre>
 * </dl>
 */
@RestController
@RequestMapping("file")
public class FileController {

    @PostMapping("/upload")
    public Result upload(@RequestParam("file")MultipartFile multipartFile){

        try {
            //1.获取文件名称  timg.jpg
            String fileName = multipartFile.getOriginalFilename();
            //2.获取文件内容
            byte[] content = multipartFile.getBytes();
            //3.获取文件扩展名 jpg
            //123123123xxxxx..jpg
            String ext = fileName.substring(fileName.lastIndexOf(".")+1);
            FastDFSFile fastDFSFile = new FastDFSFile(fileName, content, ext);


            String[] uploadResults = FastDFSClient.upload(fastDFSFile);
            //获取组名
            String groupName = uploadResults[0];
            //获取文件存储路径
            String remoteFileName = uploadResults[1];

            //文件上传完毕后的真正位置
            String path = FastDFSClient.getTrackerUrl() + groupName + "/" +remoteFileName;
            System.out.println("文件上传完毕后的路径： " + path);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result();
    }
}
